﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace DoPostForm
{
    public partial class FrmPost : Form
    {
        public FrmPost()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 完成Post请求动作
        /// </summary>
        /// <param name="url">post的action</param>
        /// <param name="body">body是要传递的参数,格式 roleId=1&uid=2 </param>
        /// <param name="contentType">post的cotentType填写  application/x-www-form-urlencoded soap填写  text/xml; charset=utf-8 </param>
        /// <returns></returns>
        string DoPostRequest(string postAction, string postData, string contentType = "application/x-www-form-urlencoded")
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postAction);

            // C# HttpRequest基础连接已经关闭: 接收时发生意外错误

            //增加下面两个属性即可  
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;


            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";

            request.ContentType = contentType;
            request.Method = "POST";
            request.Timeout = 20000;

            byte[] btBodys = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = btBodys.Length;
            request.GetRequestStream().Write(btBodys, 0, btBodys.Length);

            HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();

            httpWebResponse.Close();
            streamReader.Close();
            request.Abort();
            httpWebResponse.Close();

            return responseContent;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //txtURI.Text = "https://llssc.net:9090/aaa?controller=default&action=menu";

            string requestUrl = txtURI.Text;

            try
            {
                Uri uri = new Uri(requestUrl);

                string baseAddress = string.Concat(uri.Scheme, "://", uri.Authority);

                string postData = uri.Query;

                if (postData.Length > 1)
                {
                    postData = postData.Substring(1);
                }

                string postAction = string.Concat(baseAddress, uri.LocalPath);

                string responseText = DoPostRequest(postAction, postData);

                txtResult.Text = responseText;
            }
            catch (Exception exc)
            {
                txtResult.Text = exc.Message;
            }
        }

        private void txtURI_TextChanged(object sender, EventArgs e)
        {
            btnSubmit.Enabled = !string.IsNullOrEmpty(txtURI.Text);
        }
    }
}
